package com.api.syh56.shengyunhui.controller;

import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.Address;
import com.api.syh56.shengyunhui.service.AddressService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 地址簿
 */
@RestController
@RequestMapping("address")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 地址簿插入
     * @param address
     * @return
     */
    @PostMapping("insert")
    public CommonResult<Object> addNewAddress(@RequestBody Address address){
//        address.setUid(Long.valueOf(BaseUserInfo.get(UserInfoEnu.id.getCode())));
        addressService.addNewAddress(address);
        return CommonResult.success(address);
    }

    /**
     * 地址簿删除
     * @param address
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> delete(@RequestBody Address address){
        addressService.removeAddress(address);
        return CommonResult.success(null);
    }

    /**
     * 地址簿更新
     * @param address
     * @return
     */
    @PostMapping("update")
    public CommonResult<Object> update(@RequestBody Address address){
        addressService.updateAddress(address);
        return CommonResult.success(null);
    }

    /**
     * 地址簿列表
     * @return
     */
    @PostMapping("selectAll")
    public CommonResult<Object> selectAll(){
        List<Address> addressList = addressService.list();
        return CommonResult.success(addressList);
    }

    /**
     * 地址簿id选择
     * @param address
     * @return
     */
    @PostMapping("selectById")
    public CommonResult<Object> selectById(@RequestBody Address address){
        return CommonResult.success(addressService.selectById(address.getId()));
    }

    /**
     * 分页查询地址簿
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping (value = "selectPageByuid")
    public CommonResult<CommonPage<Address>>  selectPageByuid(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        String id = BaseUserInfo.get(UserInfoEnu.id.getCode());
        Page<Address> addressPage = addressService.selectPageByuid(Long.valueOf(id),pageSize, pageNum);
        System.out.println(addressPage);

        return CommonResult.success(CommonPage.restPage(addressPage));
    }

    /**
     * 分页查询地址簿(包含 关键词查询）
     * @param pageSize
     * @param pageNum
     * @return
     */

    @GetMapping (value = "selectPageByuidBykeyword")
    public CommonResult<CommonPage<Address>>  selectPageByuidBykeyword(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                       @RequestParam(value = "keyword", defaultValue = "") String keyword) {

        String id = BaseUserInfo.get(UserInfoEnu.id.getCode());
        Page<Address> addressPage = null;
        if (keyword.equals("")){
            addressPage = addressService.selectPageByuid(Long.valueOf(id),pageSize, pageNum);
        }else {
            addressPage = addressService.selectPageByuidBykeyword(Long.valueOf(id),pageSize, pageNum,keyword);
        }
        System.out.println(addressPage);

        return CommonResult.success(CommonPage.restPage(addressPage));
    }



}
